using Agron.Data.Contracts;
using Agron.Security.ClientLayer;
using Agron.Services.Proxies;

using System;
using System.ComponentModel;
using System.ServiceModel;
using System.Windows.Forms;
using System.Collections.Generic;
using Agron.Services.SessionController;


namespace Agron.UI.SmartClient
{
    partial class BrokerProgressForm : Form
    {
        private bool _joining;
        private Form _caller;
        private SessionModeForm _sessionModeForm;

        /// <summary>
        /// Initializes a new instance of the <see cref="BrokerProgressForm"/> class.
        /// </summary>
        public BrokerProgressForm()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Starts the session.
        /// </summary>
        /// <param name="caller">The caller.</param>
        /// <param name="session">The session.</param>
        public static void StartSession(Form caller, Session session)
        {
            BrokerProgressForm form = new BrokerProgressForm();
            form._joining = true;
            form._caller = caller;
            form._caller.Hide();
            form.progressLabel.Text = Properties.Resources.BrokerProgressFormStartingSession;
            form.Show();

            form._sessionModeForm = new SessionModeForm();
            form.backgroundWorker.RunWorkerAsync(session);
        }

        public static void StopSession(Form caller)
        {
            BrokerProgressForm form = new BrokerProgressForm();
            form._joining = false;
            form._caller = caller;
            form._caller.Hide();
            form.progressLabel.Text = Properties.Resources.BrokerProgressFormEndingSession;
            form.Show();

            form.backgroundWorker.RunWorkerAsync();
        }

        /// <summary>
        /// Gets an overridden version of the CreateParams property that contains the required creation parameters for when the control handle is created.
        /// </summary>
        /// <value>
        /// An overriden version of the <see cref="CreateParams"/> property that contains the required creation parameters for when the control handle is created.
        /// </value>
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parameters = base.CreateParams;

                if (MainForm.IsWindowsXpOrAbove)
                    parameters.ClassStyle = (parameters.ClassStyle | Constants.CS_DROPSHADOW);

                return parameters;
            }
        }

        /// <summary>
        /// Handles the Tick event of the timeoutTimer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void timeoutTimer_Tick(object sender, EventArgs e)
        {
            MessageBoxService.ReportError(this, Properties.Resources.BrokerProgressFormTimeout);
            this.Close();
        }

        /// <summary>
        /// Handles the DoWork event of the backgroundWorker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (this._joining)
            {
                Session session = (e.Argument as Session);
                SessionManager.Instance.JoinSession(this._sessionModeForm, session);
            }
            else
            {
                SessionManager.Instance.LeaveSession();
            }
        }

        /// <summary>
        /// Handles the RunWorkerCompleted event of the backgroundWorker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.RunWorkerCompletedEventArgs"/> instance containing the event data.</param>
        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this._caller.Close();

            if (null == e.Error)
            {
                if (this._joining)
                {
                    this._sessionModeForm.Show();
                }
                else
                {
                    foreach (Form form in Application.OpenForms)
                    {
                        if (form is MainForm)
                        {
                            form.Show();
                            break;
                        }
                    }
                }
            }
            else
            {
                MessageBoxService.ReportError(this, e.Error.Message);
            }

            this.Close();
        }
    }
}